﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNA_Game_Framework
{
    public class DropMenu : Control
    {
        int index;
        internal List<MenuItem> menuItems;

        SpriteFont menuFont;
        internal Texture2D topBorder;
        internal Texture2D sideBorders;
        internal Texture2D bottomBorder;
        internal Texture2D selectionBox;

        public int CurrentIndex
        {
            get { return index; }
        }

        public SpriteFont Font
        {
            get { return menuFont; }
            set { menuFont = value; }
        }

        public string GetEntry(int index)
        {
            return menuItems[index].Text;
        }

		public MenuItem GetMenuItem(int index)
		{
			return menuItems[index];
		}

        public override Vector2 Size
        {
			get { return new Vector2(sideBorders.Width, topBorder.Height + selectionBox.Height * menuItems.Count + bottomBorder.Height); }
        }

        public DropMenu(Object parent, InputHandler inputHandler, string name, Vector2 position, string[] entries, SpriteFont font,
            Texture2D topBorder, Texture2D sideBorders, Texture2D bottomBorder, Texture2D selectionBox)
            : base(name, parent, position)
        {
			//visible = false;

			menuItems = new List<MenuItem>();
            CreateItems(inputHandler, entries);
            Font = font;
            index = 0;

            this.selectionBox = selectionBox;
            this.topBorder = topBorder;
            this.sideBorders = sideBorders;
            this.bottomBorder = bottomBorder;

			inputHandler.UpKeyPressed += DecreaseIndex;
			inputHandler.DownKeyPressed += IncreaseIndex;
			inputHandler.EnterKeyPressed += ItemChosen;
        }

        private void CreateItems(InputHandler inputHandler, string[] items)
        {
            for (int i = 0; i < items.Count(); i++)
            {
                string text = items[i];
                menuItems.Add(new MenuItem(inputHandler, this, text));
            }
        }

		private void DecreaseIndex(object sender, KeyPressedEventArgs e)
		{
			index -= 1;
			if (index < 0)
			{
				index = menuItems.Count() - 1;
			}
		}

		private void IncreaseIndex(object sender, KeyPressedEventArgs e)
		{
			index += 1;
			if (index >= menuItems.Count())
			{
				index = 0;
			}
		}

		private void ItemChosen(object sender, KeyPressedEventArgs e)
		{
			menuItems[index].OnItemClicked();
		}

		override internal bool OnLeftClick(MouseEventArgs e)
		{
            if (MouseCursorIsInsideMenu(e) && Enabled && Visible)
			{
				menuItems[index].OnItemClicked();
				return true;
			}
			return false;
		}

		override internal bool OnMouseMoved(MouseEventArgs e)
		{
			if (IsWithinBounds(e.Position) && Enabled && Visible)
			{
				int highestIndex = menuItems.Count - 1;
				float positionWithinMenu = e.Y - ScreenPosition.Y - topBorder.Height;
				int calculatedIndex = (int)(positionWithinMenu / selectionBox.Height);

				index = (int)Math.Min(calculatedIndex, highestIndex);
				return true;
			}
			else
			{
                ((Container)parent).Visible = false;
				return false;
			}
		}

		private bool MouseCursorIsInsideMenu(MouseEventArgs e)
		{
			return Bounds.Contains(new Point((int)e.Position.X, (int)e.Position.Y));
		}

        internal override void Draw(SpriteBatch spriteBatch)
        {
			if (Visible == true)
			{
				DropMenuDraw.Start(this, spriteBatch);
			}
        }
    }
}
